﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data;
using Entetities;
using System.Data;
using System.Data.SqlClient;
using Entetities;
using System.Web;

namespace DAL
{
   public class SanPhamDAL
    {

       private DataHelper dh;
       public SanPhamDAL()
       {
           dh = new DataHelper();
       }

       public DataTable laysanphamtheoIDsql(int masp)
       {
           SqlParameter para = new SqlParameter("masp", masp);
           DataTable bang = hp.FieldSortTableSP("sp_laysanpham_ID", para);
           
           return bang;

       }

       public SanPham getSanPhamEntity(int ma)
       {
           DataTable dt = new DataTable();
           SqlParameter para = new SqlParameter("@masanpham", ma);
           dt = dh.FieldSortTableSP("sanpham_getsanpham", para);
           SanPham entity = null;

           if (dt != null && dt.Rows.Count > 0)
           {
               DataRow row = dt.Rows[0];
               entity = new SanPham();
               entity.Masanpham = (int)row["Masanpham"];
               entity.Tensanpham = row["Tensanpham"].ToString();
               entity.Giaban = (decimal)row["Giaban"];
               entity.Motasanpham = row["Motasanpham"].ToString();
               entity.Manhom = (int)row["Manhom"];
               entity.Manhasanxuat = (int)row["Manhasanxuat"];
               entity.Tonkho = (int)row["Tonkho"];

               return entity;
           }
           return null;
       }

       DataHelper hp = new DataHelper();

       public SanPham Epdoituong(DataRow row)
       {
           SanPham sp = new SanPham();
           sp.Masanpham=(int)row["masanpham"];
           sp.Tensanpham = (string)row["Tensanpham"];
           sp.Giaban = (decimal)row["Giaban"];
           sp.Motasanpham = (string)row["Motasanpham"];
           sp.Manhom = (int)row["Manhom"];
           sp.Manhasanxuat = (int)row["Manhasanxuat"];
           sp.Tonkho = (int)row["Tonkho"];
           return sp;
       }
       public SanPham laysanphamtheoID(int masp)
       {
           SqlParameter para = new SqlParameter("masp",masp);
           DataTable bang = hp.FieldSortTableSP("sp_laysanpham_ID", para);
           SanPham sp = null;
           if (bang.Rows.Count > 0)
           {
               DataRow row = bang.Rows[0];
               sp = Epdoituong(row);

               return sp;

           }
           return null;
       }
       public List<SanPham> getSanPhamTheoNSX(int mansx)
       {
           List<SanPham> list = new List<SanPham>();
           SqlParameter para = new SqlParameter("@manhasanxuat", mansx);
           SqlDataReader sqlreader = dh.ExecuteReader("sanpham_getsanphamNSX", para);
           while (sqlreader.Read())
           {
               SanPham oj = new SanPham();
               oj.Masanpham = (int)sqlreader["Masanpham"];
               oj.Tensanpham = sqlreader["Tensanpham"].ToString();
               oj.Giaban = decimal.Parse(sqlreader["Giaban"].ToString());
               oj.Motasanpham = sqlreader["Motasanpham"].ToString();
               oj.Tonkho = int.Parse(sqlreader["Tonkho"].ToString());
               list.Add(oj);
           }
           return list;
       }
       public DataTable getSanPhamHot()
       {

           DataTable bang = hp.FieldSortTableSP("sp_getsanpham_hot");
           return bang;
       }

       public DataTable getSanPhamcunghang(int manhom,int masp)
       {
           DataTable bang = null;
           try
           {
               SqlParameter para1 = new SqlParameter("manhom", manhom);
                SqlParameter para2=new SqlParameter("masp",masp);
                bang = hp.FieldSortTableSP("sp_getsanphamcunghang", para1, para2);
           }
           catch (Exception)
           {
               throw;
           }
           return bang;
       }

       public DataTable getsanpham(int manhom)
       {
           DataTable dt = null;
           try
           {
               SqlParameter para = new SqlParameter("@manhom", manhom);
               dt = dh.FieldSortTableSP("sanpham_gettheonhom", para);
           }
           catch (Exception)
           {
               throw;
           }
           return dt;
       }

       public void updateLuotxem(int masp)
       {
           DataService db = new DataService();
           try
           {
               db.CapnhatDulieu("update SanPham set Luotxem=Luotxem+1 where Masanpham='"+masp+"'");


           }
           catch (Exception ex)
           {
               throw;
           }


       }
       public DataTable luuseesion(int masp)
       {
           DataService db = new DataService();
           DataTable bang = db.layDulieu("select  h1.masanpham,tensanpham,Giaban,h3.Hinhthunho from Sanpham h1 join SanPham_HinhAnh h2 on h1.Masanpham=h2.Masanpham join HinhAnh h3 on h2.Mahinhanh=h3.Mahinhanh where h2.Hinhchinh=1 and h1.Masanpham='"+masp+"'");
           
           return bang;
       }
       //public DataTable getListSanPham(string sql, string soluong)
       //{
       //    DataTable dt = null;
       //    try
       //    {
       //        SqlParameter para = new SqlParameter("@dieukien", sql);
       //        SqlParameter para1 = new SqlParameter("@soluong", soluong);
       //        dt = dh.FieldSortTableSP("sanpham_gettheoboloc", para, para1);
       //    }
       //    catch (Exception)
       //    {
       //        throw;
       //    }
       //    return dt;
       //}

      

    }
 
          

}

       

    


     
    


